﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Store_Keeper_Addnewproduct : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
      
        Label3.Visible =false;
       string result= Request.QueryString.ToString();
       if (result !="")
       {
           Label3.Visible = true;
                     Label3.Text = "Product Added Sucessfully!!";
       }
        int i = Bestlife.ERP.BusinessRules.productBV_PV.lastproid();
        Productid.Text= i.ToString();
        Session["current"] = "Product";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }

        if (!IsPostBack)
        {
            pointvalue.Text = "0";
         
        }
    }

    //Update
    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {

            Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();

            pro.Point_Value = Convert.ToDecimal(e.NewValues["Point_Value"]);
            pro.Cat_ID = Convert.ToString(e.NewValues["Cat_ID"]);
            pro.Pro_ID = Convert.ToString(e.OldValues["Pro_ID"]);
            pro.Pro_Code = Convert.ToString(e.NewValues["Code"]);
            pro.Pro_Des = Convert.ToString(e.NewValues["Description"]);
            pro.Pro_Name = Convert.ToString(e.NewValues["Pro_Name"]);
            pro.Pro_Price = Convert.ToDecimal(e.NewValues["Pro_Price"]);
           
            pro.Pur_Price = Convert.ToDecimal(e.NewValues["Pur_Price"]);
            pro.Ret_Price = Convert.ToDecimal(e.NewValues["Ret_Price"]);
            pro.Brand = Convert.ToString(e.NewValues["brand"]);
            pro.Sub_ID = Convert.ToString(e.NewValues["Sub_Cat"]);
            pro.Bussiness_Value = Convert.ToDecimal(e.NewValues["Business_Value"]);
            pro.Brand = Convert.ToString(e.NewValues["brand"]);
            Bestlife.ERP.BusinessRules.productBV_PV.pvbv(pro, user);
           
        }
        catch
        {
            Label3.Text = "Please Check the data you Entered is in correct format";
        }
    }

    //Insert
    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        
        string user = Session["username"].ToString();
        try
        {
              decimal pur=(decimal)(double.Parse(purchaseprice.Text));
             decimal ret=(decimal)(double.Parse(retailprice.Text));
             if (pur <= ret)
             {
                 if (purchaseprice.Text == "0.00" || retailprice.Text == "0.00")
                 {
                     Label3.Visible = true;
                     Label3.Text = "Prices Cannot be Zero";
                 }
                 else
                 {
                     string[] imageArr = new string[4];

                     Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();

                     pro.Cat_Name1 = DropDownList1.Text;
                     pro.Point_Value = (decimal)(double.Parse(pointvalue.Text));
                     pro.Pro_Code = ASPxComboBox1.Text;
                     pro.Pro_Des = productdescription.Text;
                     pro.Pro_ID = Productid.Text;
                     pro.Pro_Name = productname.Text;
                     pro.Pro_Price = (decimal)(double.Parse(productprice.Text));
                     pro.Bussiness_Value = (decimal)(double.Parse(businesspoint.Text));
                     pro.Pur_Price = (decimal)(double.Parse(purchaseprice.Text));
                     pro.Ret_Price = (decimal)(double.Parse(retailprice.Text));
                     pro.Sub_ID = DropDownList2.Text;
                     pro.Brand = brand.Text;
                     pro.Status = DropDownList3.Text;
                     Bestlife.ERP.BusinessRules.productBV_PV.insert(pro, user);

                     //Images Upload
                     // Get the HttpFileCollection
                     if (FileUpload1 != null)
                     {
                         HttpFileCollection hfc = Request.Files;
                         for (int i = 0; i < hfc.Count; i++)
                         {
                             HttpPostedFile hpf = hfc[i];
                             if (hpf.ContentLength > 0)
                             {
                                 hpf.SaveAs(MapPath("~/C:/inetpub/vhosts/bestlifecrown.com/httpsdocs/web/img/productimage") + "\\" +
                                   System.IO.Path.GetFileName(hpf.FileName));
                                 string image = hpf.FileName;
                                 imageArr[i] = image;

                             }
                         }
                         string pid = Productid.Text;
                         Bestlife.ERP.BusinessRules.productBV_PV.insertimage(imageArr, pid);
                     }

                     string msg = Label3.Text;
                     Response.Redirect("Addnewproduct.aspx?a=msg");


                 }
             }
             else
             {
                 Label3.Visible = true;
                 Label3.Text = "Purchase Price Should be less than Retail Price";
             }
        }
        catch (Exception)
        {

            Label3.Visible = true;
            Label3.Text = "This product is alredy entered to system";
        }
    }

   
    //cancle button
    protected void ASPxButton3_Click(object sender, EventArgs e)
    {
        try
        {
            pointvalue.Text = "";
            
            productdescription.Text = "";
            Productid.Text = "";
            productname.Text = "";
            productprice.Text = "";
       
            purchaseprice.Text = "";
            retailprice.Text = "";
        }
        catch
        {
            Label3.Text = "Cannot Reset.Please try Again.";
        }

    }

    


    //PV,BV,Dispro
    protected void retailprice_TextChanged(object sender, EventArgs e)
    {
        try
        {
          
              decimal pur=(decimal)(double.Parse(purchaseprice.Text));
             decimal ret=(decimal)(double.Parse(retailprice.Text));
             if (pur <= ret)
             {
                 if (purchaseprice.Text == "0.00" || retailprice.Text == "0.00")
                 {
                     Label3.Visible = true;
                     Label3.Text = "Prices Cannot be Zero";
                 }
                 else
                 {
                     string user = Session["user"].ToString();
                     string proid = Productid.Text;
                     decimal markprice = (decimal)(double.Parse(retailprice.Text));
                     decimal cost = (decimal)(double.Parse(purchaseprice.Text));
                     decimal disprice = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice);
                     decimal pv = Bestlife.ERP.BusinessRules.productBV_PV.setPV(cost, disprice);
                     decimal bv = Bestlife.ERP.BusinessRules.productBV_PV.setBV(cost, disprice);
                     businesspoint.Text = bv.ToString();
                     pointvalue.Text = pv.ToString();
                     productprice.Text = disprice.ToString();
                     Bestlife.ERP.BusinessRules.productBV_PV.insertpvbv(cost, markprice, proid, user);
                 }
             }
             else
             {
                 Label3.Visible = true;
                 Label3.Text = "Purchase Price Should be less than Retail Price";
             }
        }
        catch
        {

        }
    }
    protected void ASPxComboBox1_Init(object sender, EventArgs e)
    {
        ASPxComboBox1.Text = "1";
    }
    
}
